<template>
  <!-- <div class="subject"> -->
  <el-card>
    <!-- 筛选表单区域 -->
    <el-row>
      <el-col :span="19">
        <el-form label-width="80px" :inline="true" class="sub-search">
          <el-form-item label="学科名称">
            <el-input v-model="queryData.subjectName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="reset" size="small">清除</el-button>
            <el-button @click="search" size="small" type="primary"
              >搜索</el-button
            >
          </el-form-item>
        </el-form></el-col
      >
      <el-col :span="5" type="flex">
        <el-button
          @click="showDialog = true"
          size="small"
          style="float: right"
          type="success"
          icon="el-icon-edit"
          >新增学科</el-button
        >
      </el-col>
    </el-row>
    <!-- 表单筛选区域 e -->
    <el-alert
      :closable="false"
      :title="'数据一共' + counts + '条'"
      type="info"
      show-icon
    >
    </el-alert>
    <!-- 表格区域 s -->
    <el-table :data="list" style="width: 100%">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="subjectName" label="学科名称"></el-table-column>
      <el-table-column prop="username" label="创建者"></el-table-column>
      <el-table-column label="创建日期" width="180px">
        <template slot-scope="{ row }">
          <span>{{ row.addDate | relativeTime }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isFrontDisplay" label="前台是否显示">
        <template slot-scope="{ row }">
          <span>{{ row.isFrontDisplay == 1 ? "是" : "否" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="twoLevelDirectory"
        label="二级目录"
      ></el-table-column>
      <el-table-column prop="tags" label="标签"></el-table-column>
      <el-table-column prop="totals" label="题目数量"></el-table-column>
      <el-table-column width="180" label="操作">
        <template slot-scope="{ row }">
          <el-button @click="toMulu(row)" type="text">学科分类</el-button>
          <el-button type="text" @click="toTag(row)">学科标签</el-button>
          <el-button type="text" @click="cgFn(row.id)">修改</el-button>
          <el-button @click="delHandler(row.id)" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域 e -->
    <!-- 分页组件 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <!-- 分页组件 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.page"
        :page-sizes="[10, 15, 20]"
        :page-size="queryData.pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="counts"
      >
      </el-pagination>
    </el-row>
    <!-- 分页组件 e -->
    <!-- 弹出层 -->
    <el-dialog
      :title="formData.id ? '修改学科' : '新增学科'"
      :visible.sync="showDialog"
      width="25%"
      @close="close"
    >
      <el-form
        v-if="showDialog"
        ref="form"
        :model="formData"
        :rules="formRules"
      >
        <el-form-item label="学科名称" prop="subjectName" label-width="80px">
          <el-input
            v-model="formData.subjectName"
            placeholder="请输入学科名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示" label-width="80px">
          <el-switch
            v-model="formData.isFrontDisplay"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button @click="addXueke" type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
  <!-- </div> -->
</template>

<script>
import {
  editXuekeApi,
  getXuekeInfoApi,
  getXuekeListApi,
  addXuekeApi,
  delXuekeApi,
} from "@/api/Xueke";
export default {
  name: "Xueke",
  data() {
    return {
      showDialog: false, //控制新增弹出层
      list: [], //存储列表
      counts: 0, //总数
      queryData: {
        //获取列表的参数
        page: 1, //页码
        pagesize: 10, //每页数量
        subjectName: "", //学科名称
      },
      formData: {
        subjectName: "", //学科名称
        isFrontDisplay: 1, //是否在页面展示 1是 0否
      }, //弹出层表单
      formRules: {
        //弹出层正则校验规则
        subjectName: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    //点击学校分类
    toMulu(row) {
      this.$router.push({
        path: "/subject/subjectmenu/?id=" + row.id + "&name=" + row.subjectName,
      });
    },
    //点击学校标签
    toTag(row) {
      this.$router.push({
        path: "/subject/subjecttags/?id=" + row.id + "&name=" + row.subjectName,
      });
    },
    //关闭弹框
    close() {
      this.formData = {
        subjectName: "", //学科名称
        isFrontDisplay: 1, //是否在页面展示 1是 0否
      };
    },
    //获取学科列表
    async initData() {
      const { counts, items } = await getXuekeListApi(this.queryData);
      this.counts = counts;
      this.list = items;
    },
    //清除搜索框按钮
    reset() {
      this.queryData = {
        page: 1, //页码
        pagesize: 10, //每页数量
        subjectName: "", //学科名称
      };
      this.initData();
    },
    //点击搜索
    search() {
      (this.queryData.page = 1),
        (this.queryData.pagesize = 10),
        this.initData();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryData.pagesize = val;
      this.initData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryData.page = val;
      this.initData();
    },
    //提交新增表单确认
    async addXueke() {
      try {
        await this.$refs.form.validate();
      } catch (error) {
        return this.$message.error("必填项不能为空");
      }
      if (this.formData.id) {
        await editXuekeApi(this.formData);
      } else {
        await addXuekeApi(this.formData);
      }
      this.$message.success("操作成功");
      this.initData();
      this.showDialog = false;
    },
    //点击修改
    async cgFn(id) {
      const res = await getXuekeInfoApi(id);
      console.log(res);
      this.formData = res;
      this.showDialog = true;
    },
    //点击删除
    async delHandler(id) {
      try {
        await this.$confirm("你确定要删除吗", "提示", {
          type: "warning",
        });
      } catch (error) {
        return this.$message("已取消删除操作");
      }
      await delXuekeApi(id);
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--;
      }
      this.initData();
      this.$message.success("删除成功");
    },
    getTime(val) {},
  },
};
</script>

<style lang="scss" scoped>
.subject {
  .sub-search {
    ::v-deep .el-form-item__label {
      font-weight: 700;
    }
  }
  ::v-deep .el-dialog__header {
    background-color: #409eff;
    .el-dialog__title {
      color: #fff;
    }
  }
  ::v-deep .el-form-item__label {
    font-weight: 700;
  }
  ::v-deep .el-dialog__close {
    color: #fff;
  }
}
</style>